package com.sq.partner.manager.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.dao.IGatewayTradCovDao;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.mybatis.model.RuleDefinition;
import com.sq.partner.manager.service.IGatewayProductService;
import com.sq.partner.manager.service.RuleConfigerService;
import com.sq.partner.manager.service.SchoolConfigService;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;

/**
 * 
 * @author 
 * 规则定义radProdInst
 */
@Controller
@RequestMapping("/ruleConfiger")
public class RuleConfigerController {
	private static Logger logger = LogUtil.getLog(RuleConfigerController.class);
	@Resource
	IGatewayProductService productService;
	@Resource
	IGatewayTradCovDao GatewayTradCovDao;
	
	@Resource
	RuleConfigerService ruleConfigerService;
	@Resource
	SchoolConfigService schoolConfigService;
	/**
	 * 跳转产品列表页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/list")
	public String productDefinition(ModelMap model,HttpServletRequest request) throws Exception {
		
		return "facadeManage/ruleConfiger/list";
	}
	/**
	 * 跳转添加页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/add")
	public String add(ModelMap model,HttpServletRequest request) throws Exception {
		return "facadeManage/ruleConfiger/add";
	}
/*	*//**
	 * 跳转编辑页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editRule")
	public String editRule(ModelMap model,HttpServletRequest request,String ruleId) throws Exception {
		model.put("ruleId", ruleId);
		return "facadeManage/ruleConfiger/edit";
	}
	/**
	 * 编辑查询列表
	 * @param size
	 * @param current
	 * @param tradProdCode
	 * @throws Exception 
	 */
	@RequestMapping(value = "/editQueryRuleList")
	public @ResponseBody Map<String,Object> editQueryRuleList(String ruleId,HttpSession session) throws Exception{
		System.out.println(ruleId);
		return ruleConfigerService.editDefinition(ruleId);
	}
	/**
	 * 查询方案列表
	 * @param size
	 * @param current
	 * @param tradProdCode
	 * @throws Exception 
	 */
	@RequestMapping(value = "/queryPlanList")
	public @ResponseBody Page<Map<String,Object>> queryPlanList(Integer size,Integer current,String planCode,String planName,HttpSession session,String partnerNo) throws Exception{
		Page<Map<String,Object>> page=new Page<Map<String,Object>>();
		page.setCurrent(current);
		page.setSize(size);
		GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("planCode", StrUtil.isEmpty(planCode)?null:planCode);
		map.put("planName", StrUtil.isEmpty(planName)?null:planName);
		map.put("partnerNo", StrUtil.isEmpty(partnerNo)?null:partnerNo);
		page.setMap(map);
		return ruleConfigerService.queryPlanPageByCriteria(page, user);
	}
	/**
	 * 查询规则列表
	 */
	@RequestMapping(value = "/queryDefinitionPage")
	public @ResponseBody Page<RuleDefinition> queryDefinitionPage(Integer size,Integer current,String partnerNo,String ruleName,String planNo,HttpSession session) throws Exception{
		Page<RuleDefinition> page=new Page<RuleDefinition>();
		page.setCurrent(current);
		page.setSize(size);
		GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("partnerNo", StrUtil.isEmpty(partnerNo)?null:partnerNo);
		map.put("ruleName", StrUtil.isEmpty(ruleName)?null:ruleName);
		map.put("planNo", StrUtil.isEmpty(planNo)?null:planNo);
		map.put("deptStr", user.getDepartmentStr());
		page.setMap(map);
		/*productService.queryPage(null);*/
		return ruleConfigerService.queryDefinitionPage(page);
	}
	
	/**
	 *  查询方案元素
	 * @param size
	 * @param current
	 * @param tradProdCode
	 * @throws Exception 
	 */
	@RequestMapping(value = "/queryTemplateList")
	public @ResponseBody Map<String, Object> queryTemplateList(String riskMain,String planCode,String prodType) throws Exception{
		
		return (Map<String, Object>) ruleConfigerService.queryTemplate(riskMain, prodType,planCode);
	}
	
	
	/**
	 * 保存模板
	 */
	@RequestMapping(value = "/saveTemplateList")
	public @ResponseBody  Page saveTemplateList(String ruleTemplate, String ruleName,String planCode,String partnerNo) throws Exception{
		Page page=new Page();
		if(StrUtil.isEmpty(ruleTemplate)){
			page.setMessage("录入元素为空");
			page.setResult("erro");
			return page;
		}
		if(StrUtil.isEmpty(ruleName)){
			page.setMessage("规则名称为空");
			page.setResult("erro");
			return page;
		}
		if(StrUtil.isEmpty(ruleName)){
			page.setMessage("方案号为空");
			page.setResult("erro");
			return page;
		}
		return ruleConfigerService.saveTemplateList(ruleTemplate, ruleName, planCode,partnerNo);
	}
	
	/**
	 * 编辑
	 * @param tradProdDesc
	 * @param tradAppType
	 */
	@RequestMapping(value = "/editTemplateList")
	public @ResponseBody  Page editTemplateList(String ruleTemplate,String ruleName,String planCode,String partnerNo,String ruleId,HttpSession session) throws Exception{
		Page page=new Page();
		GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		logger.info("用户"+user.getUserCode()+"执行编辑"+"规则iD为"+ruleId);
		if(StrUtil.isEmpty(ruleTemplate)){
			page.setMessage("录入元素为空");
			page.setResult("erro");
			return page;
		}
		if(StrUtil.isEmpty(ruleName)){
			page.setMessage("规则名称为空");
			page.setResult("erro");
			return page;
		}
		if(StrUtil.isEmpty(ruleName)){
			page.setMessage("方案号为空");
			page.setResult("erro");
			return page;
		}
		if(StrUtil.isEmpty(ruleId)){
			page.setMessage("规则id为空");
			page.setResult("erro");
			return page;
		}
		return ruleConfigerService.editTemplateList(ruleTemplate, ruleName, planCode, partnerNo, ruleId);
	}
	
	/**
	 * 删除
	 * @param tradProdDesc
	 * @param tradAppType
	 * @param prodInstCode
	 */
	@RequestMapping(value = "/deletRule")
	public @ResponseBody  Page deletRule(String ruleId,HttpSession session) throws Exception{
		Page page=new Page();
		if(StrUtil.isEmpty(ruleId)){
			page.setMessage("参数有误，请联系管理员");
			page.setResult("erro");
			return page;
		}
		GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		logger.info("用户"+user.getUserCode()+"执行删除"+"规则iD为"+ruleId);
		return ruleConfigerService.deleteRuleByid(ruleId);
	} 
}
